package net.woggle.shackbrowseapi;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.*;

import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.parser.Parser;
import org.jsoup.select.Elements;

import com.google.appengine.labs.repackaged.org.json.JSONArray;
import com.google.appengine.labs.repackaged.org.json.JSONException;
import com.google.appengine.labs.repackaged.org.json.JSONObject;


@SuppressWarnings("serial")
public class Replies extends HttpServlet {
	
	static final String LOGIN_URL = "https://www.shacknews.com/account/signin";
	static final String PAGE_URL = "http://www.shacknews.com/chatty";
	static final String USER_AGENT = "shackbrowseAPI/1.0";
	
	class Reply
	{
		String body;
		String category;
		String postId;
		String author;
		String date;
		int depth = 0;
	}
	
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		
		resp.setContentType("application/json; charset=UTF-8");
		String id = req.getParameter("id");
		
		if (id == null)
			return;
		
		Document doc = Jsoup.parse(LoginDownload.get("http://www.shacknews.com/frame_chatty.x?root=" + id));
		int num = (doc.select("body").get(0).children().size() - 1);
		if (num < 0)
			num = 0;
		resp.getWriter().print(num);
		
	}
}
